package com.antler.smt.portal.service.impl;

import com.antler.smt.entity.ProposalEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.ProposalEntityMapper;
import com.antler.smt.request.admin.GetProposalListReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.ProposalService;
import com.antler.smt.utils.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 意见反馈接口实现
 *
 * @author zhangyi
 * @date 2018/3/21 14:12
 */
@Service
public class ProposalServiceImpl implements ProposalService {

  @Autowired
  public ProposalEntityMapper proposalEntityMapper;


  /**
   *  创建意见反馈实体
   */
  ProposalEntity proposalEntity = new ProposalEntity();

  /**
   * 插入意见反馈
   * @param proposal 请求实体
   * @return
   */
  @Override
  public Result<ResultCodeEnum> insertProposalByMember(GetProposalListReq proposal,Long memberId) {

    proposalEntity.setMemberId(memberId);
    proposalEntity.setRandomId("null");

    proposalEntity.setContent(proposal.getContent());

    return isInsert(proposalEntity);
  }

  /**
   *  游客插入
   *
   * @param proposal
   * @return
   */
  @Override
  public Result<ResultCodeEnum> insertProposalRand(GetProposalListReq proposal) {

    proposalEntity.setContent(proposal.getContent());

    proposalEntity.setMemberId(Long.parseLong("0"));
    //游客随机分配id
    proposalEntity.setRandomId(RandomCodeUtil.getRandomCode(10));

    return isInsert(proposalEntity);
  }

  /**
   * 插入意见反馈
   *
   * @param proposalEntity
   * @return
   */
  Result<ResultCodeEnum> isInsert(ProposalEntity proposalEntity){

    //设置创建时间
    proposalEntity.setCreateTime(new Date(System.currentTimeMillis()));
    //设置更新时间
    proposalEntity.setUpdateTime(new Date(System.currentTimeMillis()));

    int status = proposalEntityMapper.insertProposal(proposalEntity);
    boolean flag = (0 == status);
    if(flag){
      return new Result(ResultCodeEnum.APPEND_DATA_ERROR);
    }else{
      return new Result(ResultCodeEnum.OPERATION_SUCCESS);
    }
  }
}
